﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LiveWebServiceBase;
using LiveWebServiceBase.ServiceUserSettings;

namespace LiveWebServiceAdmin.ActionPages
{
    public partial class managesite : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            if (!string.IsNullOrEmpty(Page.Request.QueryString["manageuser"]) &&
                !string.IsNullOrEmpty(Page.Request.QueryString["manageorganization"]) &&
                !string.IsNullOrEmpty(Page.Request.QueryString["editsite"]) /*&&
                Session["IsProcessed"] == null*/
                                                )
            {
                tbNewSiteID.Text = Page.Request.QueryString["editsite"];

                btnCreateNewSiteID.Text = "Save";
            }
        }

        protected void btnCreateNewSiteID_Click(object sender, EventArgs e)
        {
            try
            {
                lblErrorTextNeSiteID.Text = "";
                string manageuser = string.Empty;

                if (!string.IsNullOrEmpty(Page.Request.QueryString["manageuser"]))
                    manageuser = Page.Request.QueryString["manageuser"];
                else if (Settings.UserInfo.IsServiceUser(this.User.Identity.Name))
                    manageuser = this.User.Identity.Name;

                string manangeorg = string.Empty;

                if (!string.IsNullOrEmpty(Page.Request.QueryString["manageorganization"]))
                    manangeorg = Page.Request.QueryString["manageorganization"];

                UserSettings usItem = new UserSettings(manageuser);

                if (newPathToCert.HasFile)
                {
                    if (string.IsNullOrEmpty(Page.Request.QueryString["editsite"]))
                        usItem.CreateSiteID(manangeorg, new SiteID(
                            tbNewSiteID.Text, tbNewCertPwd.Text, newPathToCert.FileBytes));
                    else
                        usItem.EditSiteID(manangeorg, Page.Request.QueryString["editsite"], new SiteID(
                            tbNewSiteID.Text, tbNewCertPwd.Text, newPathToCert.FileBytes));
                }
                else
                {
                    throw new Exception("Can't find certificate.");
                }
                IsCompleted.Value = "true";
            }
            catch (Exception ex)
            {
                lblErrorTextNeSiteID.Text = ex.Message;
            }
        }
    }
}
